package com.jin.dto.validators;

import com.jin.dto.TokenGetDTO;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class TokenPasswordValidator implements ConstraintValidator<TokenPassword, String>{
    private int min;
    private int max;
    @Override
    //获取校验的参数
    public void initialize(TokenPassword constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    //结合校验参数判断用户传过来的是否合法
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isEmpty(s)){
            //小程序不传入密码也行
            return true;
        }
        return s.length() >= this.min && s.length() <= this.max;
    }
}
